using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Repostisory;
using ArticleManagementSystem.Api.Utils;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[Controller]")]
    public class FileUploadProcessingController : ControllerBase
    {
        private readonly IRepostisory<Imgs> _imgsRepostisory;
        private readonly IWebHostEnvironment _iWebHostEnvironment;


        public FileUploadProcessingController(IRepostisory<Imgs> imgsRepostisory, IWebHostEnvironment iWebHostEnvironment)
        {
            _imgsRepostisory = imgsRepostisory;
            _iWebHostEnvironment = iWebHostEnvironment;
        }

        [HttpPost, Route("FileUpload")]
        public dynamic FileUpload()
        {
            var file = Request.Form.Files;
            var filePath = FilesHelper.UpImgs(file, _iWebHostEnvironment, _imgsRepostisory);
            var sql = new Imgs
            {
                ImgsPath = filePath
            };
            _imgsRepostisory.Insert(sql);
            return sql;
        }

        [HttpGet,Route("imgs")]
        public dynamic Getimgs(){
            var imgs =  _imgsRepostisory.Table;

            return new {
                imgs
            };
        }
    }
}